package com.atguigu.juc.chap07;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {

    public static void main(String[] args) throws InterruptedException {

        BlockingQueue<String> synchronousQueue = new SynchronousQueue<>();
        // synchronousQueue.put("a"); // 不存储元素的阻塞队列
//        System.out.println(synchronousQueue.take()); //null

        new Thread(() -> {
            try {
                String data = UUID.randomUUID().toString().substring(0, 8);
                System.out.println("生产: " + data);
                synchronousQueue.put(data);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "生产者线程").start();


        new Thread(() -> {
            try {
                String data = synchronousQueue.take();
                System.out.println(Thread.currentThread().getName()
                        + " 消费: " + data);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "消费者线程").start();
    }
}